package cn.fan.service;

import cn.fan.constants.DataBaseConstants;
import cn.fan.feign.clients.FundClient;
import cn.fan.feign.pojo.FundMain;
import cn.fan.redis.FundFollowRedis;
import cn.fan.mapper.FundFollowMapper;
import cn.fan.pojo.FundFollow;
import cn.fan.utils.R;
import cn.fan.utils.UserUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FundFollowService {
    @Autowired
    private FundFollowRedis fundFollowRedis;

    @Autowired
    private FundFollowMapper fundFollowMapper;

    @Autowired
    private FundClient fundClient;

    public int add(FundFollow fundFollow){
        long sysUserId = UserUtils.getSysUserId();
        if (sysUserId == UserUtils.FAIL)
            return UserUtils.FAIL;

        List<FundFollow> list = fundFollowRedis.getList(sysUserId);
        for (FundFollow follow:list) {
            if(follow.getFundcode().equals(fundFollow.getFundcode()))
                return DataBaseConstants.FAIL; // 已存在
        }

        fundFollow.setUserid(sysUserId);
        fundFollow.setCreateDate(new Date());

        int i=fundFollowMapper.insert(fundFollow);
        fundFollowRedis.putById(fundFollow.getId(),fundFollow);
        fundFollowRedis.putList(sysUserId);
        return i;
    }

    public int add(String fundCode){
        R r = fundClient.getByCode(fundCode);
        if(!r.get("code").toString().equals("0"))
            return DataBaseConstants.NO_DATA;

        r.get("fundMain");
        String fundMainJson = JSON.toJSONString(r.get("fundMain"));
        FundMain fundMain = JSONObject.parseObject(fundMainJson,FundMain.class);

        FundFollow fundFollow=new FundFollow();
        fundFollow.setFundcode(fundMain.getCode());

        return add(fundFollow);
    }

    public int delete(int id){
        FundFollow follow = fundFollowRedis.getById(id);
        if (follow==null)
            return DataBaseConstants.NO_DATA;

        Long userid = follow.getUserid();

        int i=fundFollowMapper.deleteById(id);
        fundFollowRedis.delete(id);
        fundFollowRedis.putList(userid);

        return i;
    }

    public List<FundFollow> list(long userId){
        List<FundFollow> list = fundFollowRedis.getList(userId);
        for (FundFollow follow:list){
            getMain(follow);
        }
        return list;
    }

    public List<FundFollow> list(){
        long sysUserId = UserUtils.getSysUserId();
        if (sysUserId == UserUtils.FAIL)
            return new ArrayList<>();
        return list(sysUserId);
    }

    public IPage<FundFollow> page(Map<String,String> map,long userId, long currentPage, long pageSize){
        IPage<FundFollow> page = new Page<>(currentPage,pageSize);

        LambdaQueryWrapper<FundFollow> queryWrapper = new LambdaQueryWrapper<FundFollow>();
        queryWrapper.eq(FundFollow::getUserid,userId);
        if(map.get("fundcode") != null && !map.get("fundcode").equals(""))
            queryWrapper.eq(FundFollow::getFundcode,map.get("fundcode"));

        if(StringUtils.hasText(map.get("fundname")) || StringUtils.hasText(map.get("fundtype")) ){
            FundMain fundMain=new FundMain();
            fundMain.setFundname(map.get("fundname"));
            fundMain.setFundtype(map.get("fundtype"));
            R r=fundClient.list(fundMain);
            if(r.get("code").equals("0")){
                String jsonString = JSON.toJSONString(map.get("list"));
                List<FundMain> list = JSONObject.parseObject(jsonString, List.class);
                if (list.size()>0){
                    String[] codes = new String[list.size()];
                    for(int i=0;i<codes.length;i++){
                        codes[i]=list.get(i).getCode();
                    }
                    queryWrapper.in(FundFollow::getFundcode,codes);
                }
            }
        }

        IPage<FundFollow> fundFollowIPage = fundFollowMapper.selectPage(page,queryWrapper);
        System.out.println("----------------------------");
        System.out.println("getTotal:"+fundFollowIPage.getTotal()+
                ",getSize:"+fundFollowIPage.getSize()+
                ",getCurrent:"+fundFollowIPage.getCurrent()+
                ",getPages:"+fundFollowIPage.getPages());
        System.out.println("----------------------------");
        List<FundFollow> records = fundFollowIPage.getRecords();
        for(FundFollow follow:records){
            getMain(follow);
        }
        System.out.println("getTotal:"+fundFollowIPage.getTotal()+
                ",getSize:"+fundFollowIPage.getSize()+
                ",getCurrent:"+fundFollowIPage.getCurrent()+
                ",getPages:"+fundFollowIPage.getPages());
        System.out.println("----------------------------");
        return fundFollowIPage;
    }

    public IPage<FundFollow> page(Map<String,String> map,long currentPage,long pageSize){
        long sysUserId = UserUtils.getSysUserId();
        if (sysUserId == UserUtils.FAIL)
            return new Page<>(currentPage,pageSize);

        return page(map,sysUserId,currentPage,pageSize);
    }

    public FundFollow getById(int id){
        FundFollow follow = fundFollowRedis.getById(id);
        getMain(follow);
        return follow;
    }

    private void getMain(FundFollow follow){
        FundMain fundMain = fundClient.selectByCode(follow.getFundcode());
        follow.setFundMain(fundMain);
    }

}
